(function () {
	
	function downloadImage(src,name) {
	    var $a = $("<a></a>").attr("href", src).attr("download", name+".png");
	    $a[0].click();
	}
	
    //查询
    jQuery('.app-btn-search').on('click',function(){
        serverData.key=jQuery("#search").val();
        serverTable.draw();
    });
    //撤销查询
    jQuery('.app-btn-cancel').on('click',function(){
        jQuery('#search').val('');
        serverData.key='';
        serverTable.draw();
    });
    
    ///下载图片
    jQuery('.downloadBtn').on('click', function () {
    	downloadImage($("#dimg").attr("src"),jQuery('#shopName').val());
    });
    
    ///批量下载图片
    jQuery('.downloadBtnP').on('click', function () {
    	$(".downloadBtnE").trigger("click");
    });

    var serverData = {};
    var serverTable;
    jQuery(document).ready(function () {
        serverTable = jQuery('#dataTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url":window.rootPath+ '/ui/evaluate/getevaluateinfopage',
                "data": function (d) {
                    return $.extend({}, d, serverData);
                }
            },
            columns: [
                {
                data: null
                },
                {
                    data: null,
                    defaultContent: "",
                    render: function (data, type, full, meta) {
                        var operate ='<button type="button" class="btn btn-sm btn-info downloadBtnE" title="下载二维码"><i class="fa fa-download"></i></button>';
                        /*'<button type="button" class="btn btn-sm btn-info searchBtn" title="查看详情"><i class="fa fa-search"></i></button>'*/
                        return operate;

                    }
                },
                {
                	data: null,
            	    render : function(data,type, full,meta) {
						var dOut;
						if(data.businesssellerBaseInfo.infoState!=undefined){
						if (data.businesssellerBaseInfo.infoState.title == '待认证') {
							dOut = '<td class="center" ><img width="100%" src="../img/authentication_in.png"/></td>';
						} else if (data.businesssellerBaseInfo.infoState.title == '认证拒绝') {
							dOut = '<td class="center" ><img width="100%" src="../img/authentication_notaccess.png"/></td>';
						} else if (data.businesssellerBaseInfo.infoState.title == '认证通过') {
							dOut = '<td class="center" ><img width="100%" src="../img/authentication_access.png"/></td>';
						} else if (data.businesssellerBaseInfo.infoState.title == '初始化') {
							dOut = '<td class="center" ><img width="100%" src="../img/authentication_init.png"/></td>';
						} else if(data.businesssellerBaseInfo.infoState.title	==null){
							dOut='';
						}
						}
						return dOut;
					}
                },
                {
                    data: 'businesssellerBaseInfo.shopName',
                    render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'businesssellerBaseInfo.shopAdress',
                    render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'businesssellerBaseInfo.managementRange',
                    render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'businesssellerBaseInfo.managementPerson.name'
                },
                {
                    data: 'evaluateLevel.title'
                },
                {
                    data: 'evaluateScale'
                },
                {
                	data: null,
                    render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return '<div id="'+data.id+'"><script>'
							+'$("#'+data.id+'").raty();$("#'+data.id+'").raty({ number: 10,readOnly: true,score: '+data.evaluateScale/10.0+'});'
							+'</script></div>';
						}
					}
                },
                {
                	data: null,
                    render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							
							var codeUrl =window.rootPath+data.businesssellerBaseInfo.codeAdress;
							 codeUrl=window.location.origin+codeUrl;
							 

							 var qrcode =jQuery('#qrcode').qrcode({
								    render: "canvas", //也可以替换为table
								    width: 300,
								    height: 300,
								    text:codeUrl
								}).hide();
							 var canvas=qrcode.find('canvas').get(0);
							return '<img width="70%" src="'+canvas.toDataURL("image/png")+'" />';
						}
					}
                }
              ],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }

                serverTable.off('click', 'tr');
                serverTable.on('click', 'tr', function (event) {
                    event = event || window.event;
                    var target = event.target || event.scrElement;
                    var data = serverTable.row(this).data();
                    if (target.className.indexOf('fa-download') != -1 || target.className.indexOf('downloadBtnE') != -1) {
                    	downloadQ(data);
                    }
                    
                    /*if (target.className.indexOf('fa-search') != -1 || target.className.indexOf('searchBtn') != -1) {
                        showDetail(data);
                    }*/

                    return;
                });
            }
        });
        
        var downloadQ=function(data){
        	
        	var codeUrl =window.rootPath+data.businesssellerBaseInfo.codeAdress;
			 codeUrl=window.location.origin+codeUrl;
			 
			 $('#qrcode').html("");
			 var qrcode =jQuery('#qrcode').qrcode({
				    render: "canvas", //也可以替换为table
				    width: 300,
				    height: 300,
				    text:codeUrl
				}).hide();
			 var canvas=qrcode.find('canvas').get(0);
			 downloadImage(canvas.toDataURL("image/png"),data.businesssellerBaseInfo.shopName);
        };
        $("#avatar").error(function() { 
        	$(this).attr("src", "../img/empty.png"); 
        	$(this).error=null;
        	});
        var showDetail=function(data){
        	if(!data) return;
        	
        	/*var avatarUrl=window.rootPath+'/a/'+'avatar/'+data.pictureAdress;
        	

        		jQuery("#avatar").prop("src",avatarUrl+'?t='+Math.random());*/
        	var purl=data.pictureAdress;
			if(purl==undefined||purl==""){
				purl="../img/nopicture.png";
			}else{
				purl=window.rootPath+'/'+purl;
			}
			$("#avatar").attr("src",purl);
        	
    		if(data.infoState!=undefined){
    			jQuery('#infoState').val(data.infoState.title);	
    		}
    		
        	jQuery('#id').val(data.id);
        	jQuery('#createTime').val(data.createTime)
        	jQuery('#latitude').val(data.latitude)
        	jQuery('#longitude').val(data.longitude)
    		jQuery('#checkDate').val(data.checkDate);
        	jQuery('#submitDate').val(data.submitDate);
    		jQuery('#shopName').val(data.shopName);	
    		jQuery('#shopAdress').val(data.shopAdress);
    		if(data.arreaManagementPerson!=undefined){
    			jQuery('#arreaManagementPerson').val(data.arreaManagementPerson.name);
    		}
    		if(data.areaInfo!=undefined){
        		jQuery('#areaInfo').val(data.areaInfo.title);
        		jQuery('#areaInfoPhone').val(data.areaInfo.value);	
    		}
    		jQuery('#areaMeasure').val(data.areaMeasure);
    		jQuery('#signPerson').val(data.signPerson);
    		jQuery('#managementRange').val(data.managementRange);
    		jQuery('#cleaningPerson').val(data.cleaningPerson);
    		if(data.managementPerson){
    			jQuery('#managementPerson').val(data.managementPerson.name);	
    		}
			jQuery('#memo').val(data.memo);
			var codeUrl =window.rootPath+data.codeAdress;
			 codeUrl=window.location.origin+codeUrl;
			 
			 $('#qrcode').html("");
			/*jQuery('#qrcode').qrcode(codeUrl);*/
			
			var qrcode =jQuery('#qrcode').qrcode({
			    render: "canvas", //也可以替换为table
			    width: 300,
			    height: 300,
			    text:codeUrl
			}).hide();
		 var canvas=qrcode.find('canvas').get(0);
			
		 $('#dimg').attr('src',canvas.toDataURL('dimg/png'));
			
    		jQuery('#checkModal').modal('show');
        }
    });
    
    
    
    
})();